<div id="sidenav">
<?php
$top_cats = view::newInstance ()->_get ( 'top_categories' );
$current_cat = View::newInstance ()->_get ( 'current_category' );
if($current_cat){
$tree = $current_cat->toRoot ();
$tree=array_reverse($tree);
$last=$tree[count($tree)-1];	
?>
<!-- NAVIGATION SORTING AREA -->
<div id="sidebar-navigation">
<div class="sidenav-box" class="s-order">
<h2 class="refine-category"><a href="#category-tree">Refine Category</a></h2>
<div class="category-tree">
<ul>
	<li>
	<a href="#all-categories">All categories</a>
	<?php 
	$count=0;
	$num=count($tree);
	foreach($tree as $item){
	$count++;
		?>
	<ul><li><a href="<?php echo $item->getURI();?>"><?php echo $item->getName();?></a>
	<?php }?>
	<?php if($last->hasSubCategories()){
		echo '<ul>';
		$childrens=$last->getSubCategories();
		foreach($childrens as $children){?>
			<li><a href="<?php echo $children->getURI();?>"><?php echo $children->getName();?></a></li>
		<?php }
		echo '</ul>';
		}?>
	
	<?php while($count>0){?>
		</li></ul>
	<?php	$count--;	} ?>
	</li>
</ul>
</div>
</div>
</div>
<?php } ?>

<!-- LOCATION SORTING AREA -->

<?php if(Params::getParam('location')){
		$location=Params::getParam('location');
		$loc_tree=$location->toRoot();
		$loc_tree=array_reverse($loc_tree);
		$last=$loc_tree[count($loc_tree)-1];
?>
<div id="sidebar-navigation">
  <div class="sidenav-box" class="s-order">
  <h2 class="refine-category"> <a href="#category-tree">Refine location</a> <a href="#category-more"> <span class="icone more">X</span> </a> </h2>
  <div class="category-tree">
    <ul>
      <li><a href="#all-categories">All Location</a>
        <?php 
        $count=0;
        foreach($loc_tree as $loc){
        	$count++;
        ?>
        <ul><li><a href="<?php echo $loc->getURI();?>"><?php echo $loc->getName();?></a>
        <?php }
		if($last->hasChildrens()){
			$childrens=$last->getChildrens();?>
        <ul>
        <?php foreach($childrens as $children){	?>
        <li><a href="<?php echo $children->getURI();?>"><?php echo $children->getName();?></a></li>
        <?php } ?>
        </ul>
       <?php }?>
       <?php while($count>0){?>
		</li></ul>
	<?php	$count--;	} ?>
	</li>
	</ul>
        

 </div>
 </div>
 </div>


<?php }else{ ?>
<?php $top_locations=view::newInstance()->_get('top_locations');?>
<div id="sidebar-navigation">
  <div class="sidenav-box" class="s-order">
  <h2 class="refine-category"> <a href="#category-tree">Refine location</a> <a href="#category-more"> <span class="icone more">X</span> </a> </h2>
  <div class="category-tree">
    <ul>
      <li> <a href="#all-categories">All Location </a>
        <ul>
        <?php foreach($top_locations as $location){?>
          <li> <a href="<?php echo $location->getURI();?>"><?php echo $location->getName(); ?></a>
            <?php if($location->hasChildrens()){?>
          <ul>
            <?php
			$childrens=$location->getChildrens();
	           foreach($childrens as $sub){?>
              <li><a href="<?php echo $sub->getURI();?>"><?php echo $sub->getName();?></a></li>
              <?php } ?>
            </ul>
            <?php }?>
          </li>
          <?php } ?>
        </ul>
      </li>
    </ul>
    <a class="s-less refine-less" href="#more">Less</a> </div>
</div>
</div>
<?php } ?>

</div>